<script setup>
  import { ref, onMounted } from 'vue'
  import { list, allread } from '@/api/message'

  const notifyList = ref([]) // 任务列表
  const isFlag = ref(false)
  const nextPage = ref(1)
  const pageSize = ref(10)
  const isEmpty = ref(false)
  // 是否还有更多数据
  const hasMore = ref(true)
  const getNotifList = async () => {
    const { code, data } = await list(201, nextPage.value, pageSize.value)
    console.log(data)
    if (code !== 200) return uni.utlis.toast('获取通知失败，稍后重试！')
    notifyList.value = [...notifyList.value, ...(data.items || [])]
    // 更新下一页页码
    nextPage.value = ++data.page
    // 是否还有更多数据
    hasMore.value = nextPage.value <= data.pages
    isEmpty.value = notifyList.value.length === 0
  }
  onMounted(() => {
    getNotifList()
  })
  // 上拉加载
  const onScrollToLower = () => {
    if (hasMore.avlue) return
    getNotifList()
  }
  // 下拉刷新
  const onScrollViewRefresh = async () => {
    isFlag.value = true
    notifyList.value = []
    nextPage.value = 1
    await getNotifList()
    isFlag.value = false
  }
  // 全部已读
  const allread1 = async () => {
    await allread(201)
    getNotifList()
  }
</script>
<template>
  <scroll-view
    @refresherrefresh="onScrollViewRefresh"
    @scrolltolower="onScrollToLower"
    class="scroll-view"
    refresher-enabled
    :refresher-triggered="isFlag"
    scroll-y
  >
    <view class="scroll-view-wrapper">
      <view class="message-action" @click="allread1">
        <text class="iconfont icon-clear"></text>
        全部已读
      </view>
      <uni-card
        v-for="notify in notifyList"
        :key="notify.id"
        :border="false"
        :is-shadow="false"
      >
        <view class="brief">{{ notify.content }}</view>
        <view class="extra">
          <text class="time">{{ notify.created }}</text>
          <navigator
            hover-class="none"
            class="link"
            :url="`/subpkg_task/detail/index?id=${notify.id}`"
            >查看详情</navigator
          >
        </view>
        <template v-slot:title>
          <view :class="{ unread: notify.isRead === '0' }" class="title"
            >您有新的运输任务</view
          >
        </template>
      </uni-card>
      <view v-if="isEmpty" class="message-blank">暂无消息</view>
    </view>
  </scroll-view>
</template>

<style lang="scss" scoped>
  @import './styles.scss';
</style>
